#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
from forms import *
from models import *
EARLIEST_YEAR = 1905
ADMISSION_MONTH = 7
BEGIN_YEAR = 1985

def get_proper_year_choices():
    
    now = datetime.datetime.now()
    year_choices = [unicode(x) for x in range(EARLIEST_YEAR, now.year + 1)]
    year_choices.reverse()
    return year_choices

def create_new_user(form_data):

    email = form_data['email']
    name = form_data['name']
    password = form_data['password']
    gender = form_data['gender']
    role_type = form_data['role_type']
    birthday = form_data['birthday']

    user = User(email = email, name = name, password = password, gender = gender,
                role_type = role_type, birthday = birthday)
    return user

def get_school_choices():
    school_choices = [(school.id, school.name) for school in School.objects.all()]
    return school_choices

def get_department_choices():
    department_choices = [(department.id, department.name) for department in Department.objects.all()]
    return department_choices

def get_proper_admission_year_choices():
    now = datetime.datetime.now()
    last_year = now.year
    if now.month > ADMISSION_MONTH:
        last_year = now.year + 1
    admission_year_choices = [(year, unicode(year) + u'年') for year in range(BEGIN_YEAR, last_year)]
    return admission_year_choices    

def get_department_options_by_school(school):
    return '\n'.join(map((lambda x: '<option value = "'+ unicode(x.id) + \
                '">' + x.name + '</option>'), school.department_set.all()))

    

